import { Op } from "sequelize";

const db = require("../db");

export default {
    air_trajectory: {
        requestOptions: {
            api: 'trajectory',
            v: '1.0',
            data: {}
        },
        middleware: async (params) => {
            if (params.all) {
                delete params.all;
                return db.trajectory.findAll();
            }
            return db.trajectory.findOne({ where: params });
        }
    },
    trajectory_xyz: {
        requestOptions: {
            api: 'trajectoryXYZ',
            v: '1.0',
            data: {}
        },
        middleware: async (params) => {
            if (params.all) {
                delete params.all;
                return db.trajectoryXYZ.findAll();
            }
            return db.trajectoryXYZ.findOne({ where: params });
        }
    },
    search: {
        requestOptions: {
            api: 'search',
            v: '1.0',
            data: {},
        },
        middleware: async (params) => {
            return db.patient.findAll({
                where: {
                    [Op.and]: params.key.split(" ").map(
                        (keyword) => ({
                            [Op.or]: [{
                                name: {
                                    [Op.substring]: keyword
                                }
                            }, {
                                ...+keyword ? { pdid: +keyword, } : {}
                            }]
                        }))
                }
            });
        }
    }
}

/*
router.get("/p",async ctx=>{
    const query=ctx.query;
    const data=await db.patient.findAll({where:query});
    ctx.responseData=data;
});

router.get("/p/:id",async ctx=>{
    const id=ctx.params.id;
    const data=await db.patient.findOne({where:{pdid:+id}});
    ctx.responseData=data;
});

router.get("/lab/:id",async ctx=>{
    const id=ctx.params.id;
    const query=ctx.query;
    const data=await db.labtest.findAll({where:{pdid:+id,...query},order:["date"]});
    ctx.responseData=data;
});

router.get("/analyze/:id",async ctx=>{
    const id=ctx.params.id;
    const query=ctx.query;
    const data=(await db.analysis.findAll({where:{pdid:+id,...query},order:["date"]}));
    ctx.responseData=data;
});

 */